package com.tmt.im.manager.service;

import com.tmt.data.jpa.service.beans.InvalidAccessToken;
import com.tmt.im.common.pojo.IMUserPoJo;
import com.tmt.im.common.store.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.configuration.CacheConfiguration;
import org.springframework.stereotype.Service;

import javax.cache.configuration.Factory;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.ExpiryPolicy;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/8/25 22:27
 * @Version 1.0
 **/
@Slf4j
@Service
public class IgniteService {

    private final Ignite ignite;

    public IgniteService(Ignite ignite) {
        this.ignite = ignite;
    }

    public boolean loadUserPoJoIntoCache(Long userId) {
        try {
            //将新用户信息放入Ignite缓存中。
            IgniteCache<Long, IMUserPoJo> userCache = ignite.cache(IMUserPoJo.CACHE_NAME);
            if (userCache != null) {
                IMUserPoJo userPoJo = userCache.get(userId);
                if (userPoJo == null){
                    throw new Exception("从数据表中将新注册的用户加载到内存中------失败");
                }else{
                    System.out.println("从数据表中将新注册的用户加载到内存中------成功");
                }
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage(), ex);
        }
        return false;
    }

    public boolean existByUserId(Long id) {
        try {
            IgniteCache<Long, IMUserPoJo> userCache = ignite.cache(IMUserPoJo.CACHE_NAME);
            if (userCache != null) {
                IMUserPoJo poJo = userCache.get(id);
                return poJo != null;
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        return false;
    }

    public void registerLogoutUser(Long userId, String userName) {
        IgniteCache<Long, String> cache = ignite.cache(Constant.LOGOUT_USER_CACHE_NAME);
        cache.put(userId, userName);
    }

    public void registerLoginUser(Long userId) {
        IgniteCache<Long, String> cache = ignite.cache(Constant.LOGOUT_USER_CACHE_NAME);
        cache.remove(userId);
    }

    public boolean isLogout(Long userId, String curUsername) {
        IgniteCache<Long, String> tmpCache = ignite.cache(Constant.LOGOUT_USER_CACHE_NAME);
        if (tmpCache != null) {
            String username = tmpCache.get(userId);
            if (Objects.equals(username, curUsername)) {
                return true;
            }
        }
        return false;
    }

    public void registerInvalidAccessToken(InvalidAccessToken invalidAccessToken){
        IgniteCache<String, String> cache = ignite.cache(Constant.INVALID_TOKEN_CACHE_NAME);
        cache.put(invalidAccessToken.getAccessToken(), invalidAccessToken.getUsername());
    }

    public boolean isInvalidToken(String accessToken){
        IgniteCache<String, String> cache = ignite.cache(Constant.INVALID_TOKEN_CACHE_NAME);
        if (cache != null) {
            return cache.get(accessToken) != null;
        }
        return false;
    }
}
